<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\AreaStation\CreateRequest;
use App\Http\Requests\Admin\AreaStation\UpdateRequest;
use App\Models\Admin\AreaStation;
use Illuminate\Http\Request;
use Style;

class AreaStationController extends CommonController{
    /**
     * 显示地区分站
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $input = arrayFilter($request->only(['keyword']));
        $areaStation = AreaStation::select('id', 'station_domain', 'station_sign', 'station_name', 'station_cid', 'station_sort')
            ->where(function($query) use ($input){
                $keyword = (!empty($input['keyword'])) ? $input['keyword'] : '';
                if(strlen($keyword) > 0){
                    $query->where('station_name', 'like', '%' . $keyword . '%');
                }
            })
            ->orderBy('station_sort', 'asc')
            ->get();
        //根据排序值组合数组
        $areaStation = setSort($areaStation,'station_sort');
        return Style::view('admin.area_station.index', compact('areaStation'));
    }

    /**
     * 添加地区分站
     * @return mixed
     */
    public function create(){
        return Style::view('admin.area_station.create');
    }

    /**
     * 添加地区分站提交
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request){
        $input = $request->only(['station_domain', 'station_sign', 'station_name','station_cid', 'station_title', 'station_keyword', 'station_description', 'station_sort']);
        $result = AreaStation::create($input);
        if($result){
            flash('添加地区分站成功', 'success');
            return redirect('admin/area_station');
        }else{
            return back()->with('errors', '地区分站添加失败，请稍后重试！');
        }
    }

    /**
     * 显示地区分站
     * @param $id
     */
    public function show($id){
    }

    /**
     * 编辑地区分站
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $area_station = AreaStation::find($id);
        return Style::view('admin.area_station.edit', compact('area_station'));
    }

    /**
     * 更新地区分站
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateRequest $request, $id){
        $input = $request->only(['station_domain', 'station_sign', 'station_name','station_cid', 'station_title', 'station_keyword', 'station_description', 'station_sort']);
        $result = AreaStation::where('id', $id)->update($input);
        if($result){
            flash('更新地区分站成功', 'success');
            return redirect('admin/area_station');
        }else{
            return redirect()->back()->with('errors', '更新地区分站失败！！');
        }
    }

    /**
     * 删除地区分站
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $result = AreaStation::where('id', $id)->delete();
        if($result){
            return response()->json(['status' => 0, 'msg' => '删除地区分站成功！']);
        }else{
            return response()->json(['status' => 1, 'msg' => '删除地区分站失败，请稍后重试！']);
        }
    }
}
